/**
 * rainbow.com Inc.
 * Copyright (c) 2014-2015 All Rights Reserved.
 */
package com.rainbow.bam.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.rainbow.bam.entity.HtAccount;
import com.rainbow.bam.entity.PaginationResponse;
import com.rainbow.bam.exception.BusinessCheckException;
import com.rainbow.bam.service.account.HtAccountService;
import com.rainbow.bam.util.RequestHandler;

/**
 * 用户管理 - Controller
 * 
 * @author HarrisonHan
 * @version $Id: HtAccountController.java, v 0.1 2015年10月26日 下午3:36:29 HarrisonHan Exp $
 */
@Controller
public class HtAccountController {

    /**
     * 账户服务接口
     */
    @Autowired
    private HtAccountService htAccountServiceImpl;

    /**
     * 账户信息列表展现
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @return 账户信息列表展现页面
     */
    @RequiresPermissions("user:query")
    @RequestMapping(value = "/account/accountlist")
    public String accountList(HttpServletRequest request, HttpServletResponse response, Model model) {
        PaginationResponse<HtAccount> paginationResponse = htAccountServiceImpl
            .findAccountsByPagination(RequestHandler.buildPaginationRequest(request, model));
        model.addAttribute("paginationResponse", paginationResponse);
        return "account/account_list";
    }

    /**
     * 新增账户页面
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @return 账户信息新增页面
     */
    @RequiresPermissions("user:add")
    @RequestMapping(value = "/account/add", method = RequestMethod.GET)
    public String addAccount(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "account/account_add";
    }

    /**
     * 新增账户页面
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @return 账户列表页面
     * @throws BusinessCheckException 
     */
    @RequiresPermissions("user:add")
    @RequestMapping(value = "/account/add", method = RequestMethod.POST)
    public String addAccountHandler(HttpServletRequest request, HttpServletResponse response,
                                    Model model) throws BusinessCheckException {
        htAccountServiceImpl.addAccount((HtAccount) RequestHandler.createBean(request,
            new HtAccount()));
        return "redirect:/account/accountlist";
    }

    /**
     * 修改账户页面
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @param accountkey 账户编码
     * @return 账户信息修改页面
     */
    @RequiresPermissions("user:edit")
    @RequestMapping(value = "/account/edit/{accountkey}", method = RequestMethod.GET)
    public String editAccount(HttpServletRequest request, HttpServletResponse response,
                              Model model, @PathVariable("accountkey") String accountkey) {
        HtAccount htAccount = htAccountServiceImpl.findAccountByKey(accountkey);
        model.addAttribute("htaccount", htAccount);
        return "account/account_edit";
    }

    /**
     * 删除账户信息
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @param accountkey 账户编码
     * @return 跳转回账户列表页面
     * @throws BusinessCheckException 
     */
    @RequiresPermissions("user:delete")
    @RequestMapping(value = "/account/delete/{accountkey}", method = RequestMethod.GET)
    public String deleteAccount(HttpServletRequest requet, HttpServletResponse response,
                                Model model, @PathVariable("accountkey") String accountkey)
                                                                                           throws BusinessCheckException {
        htAccountServiceImpl.deleteAccount(accountkey);//删除账户
        return "redirect:/account/accountlist";
    }
    
    /**
     * 账户详情
     * 
     * @param request HttpServletRequest对象
     * @param response HttpServletResponse对象
     * @param model SpringFramework Model对象
     * @param accountkey 账户编码
     * @return 账户详情页面
     * @throws BusinessCheckException
     */
    @RequestMapping(value = "/account/profile/{accountkey}", method = RequestMethod.GET)
    public String profileAccount(HttpServletRequest request, HttpServletResponse response,
                                 Model model, @PathVariable("accountkey") String accountkey)
                                                                                            throws BusinessCheckException {
        return "account/account_profile";
    }
}
